home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / udos / fgrep103 / search.cpp < prev   
Encoding:
C/C++ Source or Header  |  1991-11-28  |  1.7 KB  |  58 lines

  1. #include "bm.h"
  2. #include "extern.h"
  3. #include <stdlib.h> /* N2 03-17-91 */
  4. #include "proto.h" /* N2 04-05-91 */
  5.  
  6. int Search(char Pattern[],int PatLen,char *EndBuff,int Skip1[],int Skip2[],
  7.     struct PattDesc *Desc)
  8.      {
  9.         char *k,         /* indexes text */
  10.              *j,         /* indexes Pattern */
  11.              *PatBegin;  /* register pointing to char
  12.                            before beginning of Pattern */
  13.         int Skip;        /* skip distance */
  14.         char *PatEnd,
  15.         *BuffEnd;        /* pointers to last char in Pattern and Buffer */
  16.         BuffEnd = EndBuff;
  17.         PatBegin = Pattern - 1;
  18.         PatEnd = Pattern + PatLen - 1;
  19.  
  20.         k = Desc->Start;
  21.         Skip = PatLen-1;
  22.         while ( Skip <= (BuffEnd - k) )
  23.          {
  24.           j = PatEnd;
  25.           k = k + Skip;
  26.           if (mFlag)
  27.            {
  28.             while ((j > PatBegin) && (*j == toupper(*k)))
  29.             {
  30.               --j; --k;
  31.             } /* while */
  32.            }
  33.           else
  34.            {
  35.             while (j > PatBegin && *j == *k)
  36.              {
  37.               --j; --k;
  38.              } /* while */
  39.            }
  40.           if (j < Pattern)
  41.            {
  42.             /* found it. Start next search just after the pattern */
  43.             Desc -> Start = k + 1 + Desc->PatLen;
  44.             return(1);
  45.            } /* if */
  46.           if (mFlag)
  47.            {
  48.             Skip = max(Skip1[toupper(*(unsigned char *)k)],Skip2[j-Pattern]);
  49.            }
  50.           else
  51.            {
  52.             Skip = max(Skip1[*(unsigned char *)k],Skip2[j-Pattern]);
  53.            }
  54.         } /* while */
  55.         Desc->Start = k;
  56.         return(0);
  57.      } /* Search */
  58.